<?php
namespace Home\Controller;
use Think\Controller;
class LearnController extends Controller {
    /**
     * 继续教育文件列表
     */
    public function showlist() {
        $M_ad   = M('learn');
        $count  = $M_ad->count();  //查出总是
        $Page   = new \Think\Page($count, 10);  // 实例化 分页类
        //定制分页类
        $Page->setConfig('header','<span class="sabrosus">共 %TOTAL_ROW% 条记录</span>'); //显示内容
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('first','首页');
        $Page->setConfig('last','末页');
        $Page->setConfig('theme'," %HEADER% %FIRST% %UP_PAGE%  %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page -> show();    
        $list       = $M_ad->limit($Page->firstRow.','.$Page->listRows)
                                    ->order('addtime DESC')
                                    ->select();
        $this->lists    = $list;
        $this->page     = $show;
        $this->display();
    }
    
    /**
     * 添加文件
     */
    public function add() {
        if(IS_POST) {
            $M_ad = M('learn');
            
            $data = I("post.");
            
            $data['addtime'] = date("Y-m-d H:i:s");
            
            $added = $M_ad->add($data);
            
            if ($added){
                $this->success("添加成功", U("showlist"));
            } else {
                $this->error("添加失败");
            }
        }  else {
            $M_learn = M('learn_sort');
            $cate = $M_learn->select();
            $this->assign('cate',$cate);
            $this->display();
        }
    }
    
    /**
     * 修改文件
     */
    public function mod() {
        $M_ad = M('learn');
        
        if(IS_POST){
            $data   = I("post.");
            //$data['addtime'] = date("Y-m-d H:i:s");

            $moded  = $M_ad->where("id = $data[id]")->save($data);
            if($moded){
                $this -> success("修改成功", U("showlist"));
            }else{
                $this -> error("修改失败");
            }
        } else {	
            $id     = I('get.id');
            $data   = $M_ad->where(array("id"=>$id))->find();
            $M_learn = M('learn_sort');
            $cate   = $M_learn->select();
            $this->assign('cate',$cate);
            $this->assign('data',$data);
            $this->display();
        }
    }
    
    /**
     * 删除文件
     */
    public function del() {
        $M_ad       = M("learn");
        $id         = I("get.id");
        $del_row    = $M_ad->where(array('id'=>$id))->delete();
        if ($del_row) {
            $this->success("删除成功", U("showlist"));
        } else {
            $this->error("删除失败");
        }
    }
    /**
     * 文件分类列表
     */
    public function sortlist() {
        $M_ad   = M('learn_sort');
        $list       = $M_ad->select();
        $this->lists    = $list;
        $this->display();
    }
    /**
     * 添加文件
     */
    public function sortadd() {
        if(IS_POST) {
            $M_ad = M('learn_sort');

            $data = I("post.");

            $data['addtime'] = date("Y-m-d H:i:s");

            $added = $M_ad->add($data);

            if ($added){
                $this->success("添加成功", U("sortlist"));
            } else {
                $this->error("添加失败");
            }
        }  else {
            $this->display();
        }
    }
    /**
     * 修改文件分类
     */
    public function sortmod() {
        $M_ad = M('learn_sort');

        if(IS_POST){
            $data   = I("post.");
            //$data['addtime'] = date("Y-m-d H:i:s");

            $moded  = $M_ad->where("id = $data[id]")->save($data);
            if($moded){
                $this -> success("修改成功", U("sortlist"));
            }else{
                $this -> error("修改失败");
            }
        } else {
            $id     = I('get.id');
            $data   = $M_ad->where(array("id"=>$id))->find();
            $this->cate_row = $data;
            $this->display();
        }
    }
    /**
     * 删除文件分类
     */
    public function sortdel() {
        $M_ad       = M("learn_sort");
        $id         = I("get.id");
        $del_row    = $M_ad->where(array('id'=>$id))->delete();
        if ($del_row) {
            $this->success("删除成功", U("sortlist"));
        } else {
            $this->error("删除失败");
        }
    }
}